#include "editorwidget.h"
#include "ui_editorwidget.h"

EditorWidget::EditorWidget(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::EditorWidget)
{
    m_ui->setupUi(this);
    m_findWidgetVisible = false;
    setFindWidgetVisible(m_findWidgetVisible);
}

EditorWidget::~EditorWidget()
{
    delete m_ui;
}

void EditorWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

EEditor* EditorWidget::editor()
{
    return m_ui->editor;
}

void EditorWidget::on_findButton_clicked()
{
    editor()->findFirst(m_ui->findEdit->text(),
                        true,
                        (m_ui->caseSensitiveComboBox->checkState()== Qt::Checked),
                        true,
                        true);
}

void EditorWidget::on_replaceBtn_clicked()
{
    if(m_ui->replaceModeCheckBox->checkState() == Qt::Checked)
    {
        while(editor()->findFirst(m_ui->findEdit->text(), true, (m_ui->caseSensitiveComboBox->checkState()== Qt::Checked), true, true))
        {
            editor()->replace(m_ui->replaceEdit->text());
        }
    }
    else
    {
        on_findButton_clicked();
        editor()->replace(m_ui->replaceEdit->text());
    }
}

void EditorWidget::on_closeButton_clicked()
{
    setFindWidgetVisible(false);
}

bool EditorWidget::isFindWidgetVisible()
{
    return m_findWidgetVisible;
}

void EditorWidget::setFindWidgetVisible(bool visibility)
{
    m_ui->findButton->setVisible(visibility);
    m_ui->findEdit->setVisible(visibility);
    m_ui->caseSensitiveComboBox->setVisible(visibility);
    m_ui->circleFindCheckBox->setVisible(visibility);
    m_ui->closeButton->setVisible(visibility);
    m_ui->replaceBtn->setVisible(visibility);
    m_ui->replaceEdit->setVisible(visibility);
    m_ui->replaceModeCheckBox->setVisible(visibility);
    m_findWidgetVisible = visibility;
}
